package com.wxj.book.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxj.book.cms.dao.BookMapper;
import com.wxj.book.cms.dao.BookVoMapper;
import com.wxj.book.cms.pojo.Book;
import com.wxj.book.cms.service.BookService;
import com.wxj.book.cms.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BookVoMapper bookVoMapper;

    /**
     * 带有分页查询书籍
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<BookVo> getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BookVo> bookVos = bookVoMapper.getAll();
        PageInfo<BookVo> pageInfo = new PageInfo<>(bookVos);
        return pageInfo;
    }

    /**
     * 更新或者添加书籍
     *
     * @param book
     * @return
     */
    @Override
    public int updateBook(Book book) {
        if (book.getId() == null) {
            Book newBook = new Book();
            newBook.setName(book.getName());
            newBook.setStatus(book.getStatus());
            newBook.setCategoryId(book.getCategoryId());
            newBook.setQuantity(book.getQuantity());
            newBook.setAuthor(book.getAuthor());
            newBook.setIsTao(book.getIsTao());
            newBook.setZz(book.getZz());
            newBook.setKb(book.getKb());
            newBook.setBz(book.getBz());
            newBook.setContent(book.getContent());
            newBook.setCbs(book.getCbs());
            newBook.setContent(book.getContent());
            newBook.setImgUrl(book.getImgUrl());
            return bookMapper.insertSelective(newBook);

        }

        Book newBook = new Book();
        newBook.setId(book.getId());
        newBook.setName(book.getName());
        newBook.setStatus(book.getStatus());
        newBook.setCategoryId(book.getCategoryId());
        newBook.setQuantity(book.getQuantity());
        newBook.setAuthor(book.getAuthor());
        newBook.setIsTao(book.getIsTao());
        newBook.setZz(book.getZz());
        newBook.setKb(book.getKb());
        newBook.setBz(book.getBz());
        newBook.setContent(book.getContent());
        newBook.setCbs(book.getCbs());
        newBook.setContent(book.getContent());
        newBook.setImgUrl(book.getImgUrl());
        return bookMapper.updateByPrimaryKeySelective(newBook);
    }
}
